package xyz.chaobei.controller.page;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import xyz.chaobei.entity.OrgPermissionEntity;
import xyz.chaobei.service.OrgPermissionService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2015-2020
 * FileName: ViewController
 * Author:   MRC
 * Date:     2020/1/15 15:56
 * Description: 页面控制器
 * History:
 */
@Controller
public class ViewController {

    @Autowired
    private OrgPermissionService orgPermissionService;

    @RequestMapping({"/index","/"})
    public String toIndex(Map<String, Object> map) {

        List<OrgPermissionEntity> result = orgPermissionService.findUserMenu();
        map.put("menu",result);
        return "index";
    }

    /**
     * 登陆页面
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, Map<String, Object> map) throws Exception {
        System.out.println("HomeController.login()");
        // 登录失败从request中获取shiro处理的异常信息。
        // shiroLoginFailure:就是shiro异常类的全类名.
        String exception = (String) request.getAttribute("shiroLoginFailure");
        System.out.println("exception=" + exception);
        String msg = "";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
                System.out.println("UnknownAccountException -- > 账号不存在：");
                msg = "UnknownAccountException -- > 账号不存在：";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
                System.out.println("IncorrectCredentialsException -- > 密码不正确：");
                msg = "IncorrectCredentialsException -- > 密码不正确：";
            } else if ("kaptchaValidateFailed".equals(exception)) {
                System.out.println("kaptchaValidateFailed -- > 验证码错误");
                msg = "kaptchaValidateFailed -- > 验证码错误";
            } else {
                msg = "else >> " + exception;
                System.out.println("else -- >" + exception);
            }
        }
        map.put("msg", msg);
        return "/login";
    }

    /**
     * 没有权限页面
     * @return
     */
    @RequestMapping("/403")
    public String unauthorizedRole() {
        System.out.println("------没有权限-------");
        return "403";
    }
    /**
     * 统计页面
     * @return
     */
    @GetMapping("/welcome")
    public String toWelcome(){
        return "welcome";
    }



}